using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WinCommand.SnippetManager
{
	/// <summary>
	/// This dialog box allows the end-user to manage a collection of snippets. It allows
	/// addition and deletion of snippets, as well as sorting and rearranging. There's more
	/// work required to achieve this than meets the eye, as can be seen below... :-)
	/// </summary>
	public class OrganizeSnippets : System.Windows.Forms.Form
	{	
		#region Declarations, constructors and deconstructors
		// This is where we temporarily store a working copy of the snippets whilst 
		// the user is working within the dialog box.
		private ArrayList snippets;
		private CategoryCollection categories;
		private int selectedCategoryIndex = 0;

		private System.Windows.Forms.Label lblIntro;
		private System.Windows.Forms.Button btnNew;
		private System.Windows.Forms.Button btnDelete;
		private System.Windows.Forms.Label lblName;
		private System.Windows.Forms.Label lblCode;
		private System.Windows.Forms.TextBox textName;
		private System.Windows.Forms.TextBox textCode;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnUp;
		private System.Windows.Forms.Button btnDown;
		private System.Windows.Forms.ComboBox comboBoxCategory;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ListView listViewSnippets;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.ComboBox comboBoxCategory2;

		private System.ComponentModel.Container components = null;


		public ArrayList Snippets
		{
			set { snippets = value; }
			get { return snippets; }
		}
		public CategoryCollection Categories
		{
			get { return categories; }
			set { categories = value; }
		}
		public int SelectedCategory
		{
			get
			{
				return selectedCategoryIndex;
			}
			set
			{
				selectedCategoryIndex = value;
			}
		}


		public OrganizeSnippets()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		}


		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#endregion

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(OrganizeSnippets));
			this.lblIntro = new System.Windows.Forms.Label();
			this.btnNew = new System.Windows.Forms.Button();
			this.btnDelete = new System.Windows.Forms.Button();
			this.lblName = new System.Windows.Forms.Label();
			this.lblCode = new System.Windows.Forms.Label();
			this.textName = new System.Windows.Forms.TextBox();
			this.textCode = new System.Windows.Forms.TextBox();
			this.btnOK = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnUp = new System.Windows.Forms.Button();
			this.btnDown = new System.Windows.Forms.Button();
			this.comboBoxCategory = new System.Windows.Forms.ComboBox();
			this.label1 = new System.Windows.Forms.Label();
			this.listViewSnippets = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.label3 = new System.Windows.Forms.Label();
			this.comboBoxCategory2 = new System.Windows.Forms.ComboBox();
			this.SuspendLayout();
			// 
			// lblIntro
			// 
			this.lblIntro.Anchor = System.Windows.Forms.AnchorStyles.None;
			this.lblIntro.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.lblIntro.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lblIntro.Location = new System.Drawing.Point(8, 8);
			this.lblIntro.Name = "lblIntro";
			this.lblIntro.Size = new System.Drawing.Size(344, 16);
			this.lblIntro.TabIndex = 0;
			this.lblIntro.Text = "Use this dialog box to organize your snippets.";
			// 
			// btnNew
			// 
			this.btnNew.Anchor = System.Windows.Forms.AnchorStyles.None;
			this.btnNew.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnNew.Location = new System.Drawing.Point(48, 192);
			this.btnNew.Name = "btnNew";
			this.btnNew.Size = new System.Drawing.Size(64, 23);
			this.btnNew.TabIndex = 2;
			this.btnNew.Text = "&New";
			this.btnNew.Click += new System.EventHandler(this.btnNew_Click);
			// 
			// btnDelete
			// 
			this.btnDelete.Anchor = System.Windows.Forms.AnchorStyles.None;
			this.btnDelete.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnDelete.Location = new System.Drawing.Point(120, 192);
			this.btnDelete.Name = "btnDelete";
			this.btnDelete.Size = new System.Drawing.Size(64, 23);
			this.btnDelete.TabIndex = 3;
			this.btnDelete.Text = "&Delete";
			this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
			// 
			// lblName
			// 
			this.lblName.Anchor = System.Windows.Forms.AnchorStyles.None;
			this.lblName.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.lblName.Location = new System.Drawing.Point(296, 80);
			this.lblName.Name = "lblName";
			this.lblName.Size = new System.Drawing.Size(32, 16);
			this.lblName.TabIndex = 4;
			this.lblName.Text = "Na&me:";
			// 
			// lblCode
			// 
			this.lblCode.Anchor = System.Windows.Forms.AnchorStyles.None;
			this.lblCode.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.lblCode.Location = new System.Drawing.Point(16, 240);
			this.lblCode.Name = "lblCode";
			this.lblCode.Size = new System.Drawing.Size(32, 23);
			this.lblCode.TabIndex = 5;
			this.lblCode.Text = "&Code:";
			// 
			// textName
			// 
			this.textName.Anchor = System.Windows.Forms.AnchorStyles.None;
			this.textName.Font = new System.Drawing.Font("Lucida Console", 8.25F);
			this.textName.Location = new System.Drawing.Point(328, 80);
			this.textName.Name = "textName";
			this.textName.Size = new System.Drawing.Size(288, 18);
			this.textName.TabIndex = 6;
			this.textName.Text = "";
			this.textName.Leave += new System.EventHandler(this.textName_Leave);
			// 
			// textCode
			// 
			this.textCode.Anchor = System.Windows.Forms.AnchorStyles.None;
			this.textCode.Font = new System.Drawing.Font("Lucida Console", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.textCode.Location = new System.Drawing.Point(48, 240);
			this.textCode.Multiline = true;
			this.textCode.Name = "textCode";
			this.textCode.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.textCode.Size = new System.Drawing.Size(568, 82);
			this.textCode.TabIndex = 7;
			this.textCode.Text = "";
			this.textCode.Leave += new System.EventHandler(this.textCode_Leave);
			// 
			// btnOK
			// 
			this.btnOK.Anchor = System.Windows.Forms.AnchorStyles.None;
			this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnOK.Location = new System.Drawing.Point(464, 328);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(72, 23);
			this.btnOK.TabIndex = 8;
			this.btnOK.Text = "OK";
			// 
			// btnCancel
			// 
			this.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.None;
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnCancel.Location = new System.Drawing.Point(544, 328);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(72, 23);
			this.btnCancel.TabIndex = 9;
			this.btnCancel.Text = "Cancel";
			// 
			// btnUp
			// 
			this.btnUp.Anchor = System.Windows.Forms.AnchorStyles.None;
			this.btnUp.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnUp.Image = ((System.Drawing.Image)(resources.GetObject("btnUp.Image")));
			this.btnUp.Location = new System.Drawing.Point(200, 80);
			this.btnUp.Name = "btnUp";
			this.btnUp.Size = new System.Drawing.Size(24, 23);
			this.btnUp.TabIndex = 10;
			this.btnUp.Click += new System.EventHandler(this.btnUp_Click);
			// 
			// btnDown
			// 
			this.btnDown.Anchor = System.Windows.Forms.AnchorStyles.None;
			this.btnDown.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnDown.Image = ((System.Drawing.Image)(resources.GetObject("btnDown.Image")));
			this.btnDown.Location = new System.Drawing.Point(200, 104);
			this.btnDown.Name = "btnDown";
			this.btnDown.Size = new System.Drawing.Size(24, 23);
			this.btnDown.TabIndex = 11;
			this.btnDown.Click += new System.EventHandler(this.btnDown_Click);
			// 
			// comboBoxCategory
			// 
			this.comboBoxCategory.Anchor = System.Windows.Forms.AnchorStyles.None;
			this.comboBoxCategory.Font = new System.Drawing.Font("Lucida Console", 8.25F);
			this.comboBoxCategory.Location = new System.Drawing.Point(40, 56);
			this.comboBoxCategory.Name = "comboBoxCategory";
			this.comboBoxCategory.Size = new System.Drawing.Size(152, 19);
			this.comboBoxCategory.TabIndex = 13;
			// 
			// label1
			// 
			this.label1.Anchor = System.Windows.Forms.AnchorStyles.None;
			this.label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label1.Location = new System.Drawing.Point(16, 32);
			this.label1.Name = "label1";
			this.label1.TabIndex = 14;
			this.label1.Text = "&Category:";
			// 
			// listViewSnippets
			// 
			this.listViewSnippets.Anchor = System.Windows.Forms.AnchorStyles.None;
			this.listViewSnippets.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																							   this.columnHeader1});
			this.listViewSnippets.Font = new System.Drawing.Font("Lucida Console", 8.25F);
			this.listViewSnippets.FullRowSelect = true;
			this.listViewSnippets.HideSelection = false;
			this.listViewSnippets.Location = new System.Drawing.Point(40, 80);
			this.listViewSnippets.MultiSelect = false;
			this.listViewSnippets.Name = "listViewSnippets";
			this.listViewSnippets.Size = new System.Drawing.Size(152, 106);
			this.listViewSnippets.TabIndex = 16;
			this.listViewSnippets.View = System.Windows.Forms.View.Details;
			this.listViewSnippets.SelectedIndexChanged += new System.EventHandler(this.listViewSnippets_SelectedIndexChanged);
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "Snippet";
			this.columnHeader1.Width = 148;
			// 
			// label3
			// 
			this.label3.Anchor = System.Windows.Forms.AnchorStyles.None;
			this.label3.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.label3.Location = new System.Drawing.Point(280, 120);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(48, 16);
			this.label3.TabIndex = 18;
			this.label3.Text = "&Category:";
			// 
			// comboBoxCategory2
			// 
			this.comboBoxCategory2.Anchor = System.Windows.Forms.AnchorStyles.None;
			this.comboBoxCategory2.Font = new System.Drawing.Font("Lucida Console", 8.25F);
			this.comboBoxCategory2.Location = new System.Drawing.Point(328, 120);
			this.comboBoxCategory2.Name = "comboBoxCategory2";
			this.comboBoxCategory2.Size = new System.Drawing.Size(288, 19);
			this.comboBoxCategory2.TabIndex = 17;
			this.comboBoxCategory2.Leave += new System.EventHandler(this.comboBoxCategory2_Leave);
			// 
			// OrganizeSnippets
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
			this.ClientSize = new System.Drawing.Size(626, 354);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.comboBoxCategory2);
			this.Controls.Add(this.listViewSnippets);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.comboBoxCategory);
			this.Controls.Add(this.btnDown);
			this.Controls.Add(this.btnUp);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.textCode);
			this.Controls.Add(this.textName);
			this.Controls.Add(this.lblCode);
			this.Controls.Add(this.lblName);
			this.Controls.Add(this.btnDelete);
			this.Controls.Add(this.btnNew);
			this.Controls.Add(this.lblIntro);
			this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.MinimumSize = new System.Drawing.Size(520, 300);
			this.Name = "OrganizeSnippets";
			this.ShowInTaskbar = false;
			this.Text = "Organize Snippets";
			this.Load += new System.EventHandler(this.OrganizeSnippets_Load);
			this.ResumeLayout(false);

		}
		#endregion


		private void OrganizeSnippets_Load(object sender, System.EventArgs e)
		{
			this.comboBoxCategory.DataSource = categories;
			this.comboBoxCategory.DisplayMember = "Name";
			this.comboBoxCategory.ValueMember = "Name";

			this.comboBoxCategory2.DataSource = (CategoryCollection)categories.Clone();
			this.comboBoxCategory2.DisplayMember = "Name";
			this.comboBoxCategory2.ValueMember = "Name";

			this.comboBoxCategory.SelectedIndex = 0;
			this.comboBoxCategory.SelectedIndexChanged += new EventHandler(this.comboBoxCategory_SelectedIndexChanged);
														
			SynchronizeView();
			btnUp.Enabled = btnDown.Enabled = false;

			this.comboBoxCategory.SelectedIndex = selectedCategoryIndex;
			this.comboBoxCategory2.SelectedIndex = selectedCategoryIndex;
		}


		


		/// <summary>
		/// Moves the currently selected snippet up one place in the collection.
		/// </summary>
		private void btnUp_Click(object sender, System.EventArgs e)
		{
			// Swap snippet with the one above it.

			if (listViewSnippets.SelectedItems.Count > 0)
			{
				Snippet selectedSnippet = (Snippet)listViewSnippets.SelectedItems[0].Tag;
				string name = selectedSnippet.Name;

				int selectedListViewIndex = listViewSnippets.SelectedIndices[0];
				// Go on if this is not the first element in ListView
				if (selectedListViewIndex > 0)
				{
					Snippet nextSnippet = (Snippet)listViewSnippets.Items[selectedListViewIndex-1].Tag;

					int selectedArryIndex = Snippets.IndexOf(selectedSnippet);
					int nextArrayIndex = Snippets.IndexOf(nextSnippet);

					snippets.Insert(nextArrayIndex, selectedSnippet);
					snippets.RemoveAt(selectedArryIndex + 1);

					SynchronizeView();

					this.SelectSnippetByName(name);

				}
			}

		}


		/// <summary>
		/// Moves the currently selected snippet down one place in the collection.
		/// </summary>
		private void btnDown_Click(object sender, System.EventArgs e)
		{

			if (listViewSnippets.SelectedItems.Count > 0)
			{
				Snippet selectedSnippet = (Snippet)listViewSnippets.SelectedItems[0].Tag;
				string name = selectedSnippet.Name;

				int selectedListViewIndex = listViewSnippets.SelectedIndices[0];
				// Go on if this is not the last element in ListView
				if ((selectedListViewIndex >= 0) && (selectedListViewIndex < listViewSnippets.Items.Count - 1))
				{
					Snippet nextSnippet = (Snippet)listViewSnippets.Items[selectedListViewIndex+1].Tag;

					int selectedArryIndex = Snippets.IndexOf(selectedSnippet);
					int nextArrayIndex = Snippets.IndexOf(nextSnippet);

					snippets.Insert(selectedArryIndex, nextSnippet);
					snippets.RemoveAt(nextArrayIndex + 1);

					SynchronizeView();

					this.SelectSnippetByName(name);

				}
			}



			// Swap snippet with the one below it.
//			int index = listSnippets.SelectedIndex;
//
//			if ((index > -1) && (index < listSnippets.Items.Count - 1))
//			{
//				snippets.Insert(index, ((Snippet)snippets[index+1]));
//				snippets.RemoveAt(index + 2);
//
//				SynchronizeView();
//				listSnippets.SelectedIndex = index + 1;
//			}
		}


		/// <summary>
		/// Deletes the currently selected snippet from the collection.
		/// </summary>
		private void btnDelete_Click(object sender, System.EventArgs e)
		{
			// -1 indicates that no item is selected
			if (listViewSnippets.SelectedItems.Count > 0)
			{
				int index = listViewSnippets.SelectedIndices[0];
				snippets.Remove((Snippet)listViewSnippets.SelectedItems[0].Tag);

				SynchronizeView();

				// Now select the next item in the list, if there is one.
				if (listViewSnippets.SelectedItems.Count > index)
				{
					listViewSnippets.Items[index].Selected = true;					
				}

				// If we deleted the last item, we won't have anything selected. 
				// So select the new last item (unless there isn't anything left, 
				// in which case we clear the text boxes to match).
				if (listViewSnippets.SelectedItems.Count == 0)
				{
					if(listViewSnippets.Items.Count > 0)
					{
						listViewSnippets.Items[listViewSnippets.Items.Count - 1].Selected = true;
					}
					else
					{
						textName.Text = textCode.Text = "";
					}
				}
			}			
		}


		/// <summary>
		/// Inserts a new snippet at the end of the collection. 
		/// </summary>
		private void btnNew_Click(object sender, System.EventArgs e)
		{

			// We insert a new item at the end and then select it			
			Snippet newSnippet = new Snippet();
			string name = SnippetData.GetUniqueName(snippets);
			newSnippet.Name = name;
			newSnippet.Code = "";
			newSnippet.Category = (string)comboBoxCategory.SelectedValue;
			snippets.Add(newSnippet);

			SynchronizeView();

			SelectSnippetByName(name);
		}


		/// <summary>
		/// Rather than attempting to manipulate both the view on the screen and the 
		/// snippets ArrayList that contains the actual data we're working with at the
		/// same time, we work with the snippets collection during each of the event handlers.
		/// Where the changes require that the screen display is updated to match the 
		/// underlying values, we call this method to resynchronize the dialog with the
		/// data. At the cost of a mild performance hit, we reduce the risk of code bugs being
		/// introduced through trying to keep both things in sync during each operation.
		/// </remarks>
		private void SynchronizeView()
		{
			this.SuspendLayout();
			listViewSnippets.Items.Clear();
			if (this.comboBoxCategory.SelectedIndex > -1)
			{
				string category = (string)this.comboBoxCategory.SelectedValue;

				foreach (Snippet s in snippets)
				{
					if (s.Category == category)
					{
						ListViewItem item = new ListViewItem(s.Name);
						item.Tag = s;
						listViewSnippets.Items.Add(item);
					}
				}

			}
			this.ResumeLayout();
		}


		/// <summary>
		/// This event fires when someone tabs away from (or clicks off) the Name textbox. 
		/// In most cases, we'll want to absorb their changes automatically into the snippets
		/// collection, but we must prevent two snippets being added with the same name.
		/// </summary>
		private void textName_Leave(object sender, System.EventArgs e)
		{
			if (listViewSnippets.SelectedItems.Count > 0)
			{
				ListViewItem item = listViewSnippets.SelectedItems[0];
			// If something has been added here, but there's nothing selected, or if
			// nothing has actually been changed, then we discard the changes...
				if (textName.Text != ((Snippet)item.Tag).Name )
				{
					// OK, so they're trying to change the name of an existing snippet.
					// Check whether there's already a column with that name in existence, 
					// since we require all snippets to have unique names.
					bool nameAlreadyTaken = false;
					foreach(Snippet s in snippets)
					{
						if (s.Name == textName.Text)
							nameAlreadyTaken = true;
					}

					if (!nameAlreadyTaken)
					{
					
						((Snippet)item.Tag).Name = textName.Text;

					
						SynchronizeView();
					
						SelectSnippetByName(textName.Text);
						
					}
					else
					{
						MessageBox.Show("A snippet with that name already exists. Please choose another name.", "", MessageBoxButtons.OK, MessageBoxIcon.Hand);
						textName.Text = ((Snippet)item.Tag).Name;
						textName.Focus();
						textName.SelectAll();
					}
				}
			}
		}

		private void comboBoxCategory2_Leave(object sender, System.EventArgs e)
		{
			if (listViewSnippets.SelectedItems.Count > 0)
			{
				int index = listViewSnippets.SelectedIndices[0];

				ListViewItem item = listViewSnippets.SelectedItems[0];
				Snippet s = (Snippet)item.Tag;
				string selectedCategory = (string)comboBoxCategory2.SelectedValue;
				if (selectedCategory != s.Category )
				{
					s.Category = selectedCategory;

					SynchronizeView();

					// Now select the next item in the list, if there is one.
					if (listViewSnippets.SelectedItems.Count > index)
					{
						listViewSnippets.Items[index].Selected = true;					
					}

					// If we deleted the last item, we won't have anything selected. 
					// So select the new last item (unless there isn't anything left, 
					// in which case we clear the text boxes to match).
					if (listViewSnippets.SelectedItems.Count == 0)
					{
						if(listViewSnippets.Items.Count > 0)
						{
							listViewSnippets.Items[listViewSnippets.Items.Count - 1].Selected = true;
						}
						else
						{
							textName.Text = textCode.Text = "";
						}
					}

				}
			}
		
		}

		/// <summary>
		/// This event fires when someone tabs away from (or clicks off) the Code textbox.
		/// Here we simply update the snippet accordingly, so long as they've actually got
		/// one selected at the time.
		/// </summary>
		private void textCode_Leave(object sender, System.EventArgs e)
		{
			if (listViewSnippets.SelectedItems.Count > 0)
			{
				ListViewItem item = listViewSnippets.SelectedItems[0];
				if (item != null)
				{
					((Snippet)item.Tag).Code = textCode.Text;
				}
			}
		}

		private void comboBoxCategory_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			SynchronizeView();
			if (listViewSnippets.Items.Count > 0)
			{
				listViewSnippets.Items[0].Selected = true;
			}
			else
			{
				textName.Text = textCode.Text = "";
			}
		}

		private void SelectSnippetByName(string name)
		{
			foreach(ListViewItem selectedItem in listViewSnippets.Items)
			{
				if (((Snippet)selectedItem.Tag).Name == name)
				{
					selectedItem.Selected = true;
					break;
				}
			}
		}

		/// <summary>
		/// Called whenever a different snippet is selected. Updates the properties area 
		/// of the window to match the currently selected snippet, and enables or disables 
		/// the up/down arrows accordingly.
		/// </summary>
		private void listViewSnippets_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (listViewSnippets.SelectedItems.Count > 0)
			{
				ListViewItem item = listViewSnippets.SelectedItems[0];
				Snippet s = (Snippet)item.Tag;
				textName.Text = s.Name;
				textCode.Text = s.Code;
								
				foreach (object categoryItem in comboBoxCategory2.Items)
				{
					if (s.Category == ((Category)categoryItem).Name)
					{
						comboBoxCategory2.SelectedItem = categoryItem;
						break;
					}
				}

				btnUp.Enabled = item.Index > 0;
				btnDown.Enabled = (item.Index >= 0) && (item.Index < listViewSnippets.Items.Count -1);

			}
			
		
		}

		
	}
}
